<template>
  <div>
    <el-button type="danger" @click="batchDelete()">批量删除</el-button>
    <el-tree
      ref="categoryTree"
      :data="data"
      show-checkbox
      :expand-on-click-node="false"
      node-key="id"
      :props="defaultProps"
      :default-expanded-keys="expandedKey"
      @node-click="handleNodeClick">
    <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-show="node.level <= 2"
            icon="el-icon-plus"
            type="text"
            size="mini"
            @click="() => append(data)">
            增加
          </el-button>
          <el-button
            type="text"
            icon="el-icon-edit"
            size="mini"
            @click="() => edit(data)">
            修改
          </el-button>
          <el-button
            v-show="node.childNodes.length == 0"
            type="text"
            size="mini"
            icon="el-icon-delete"
            @click="() => remove(node, data)">
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
    <!--  添加分类弹窗-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="categoryForm">
        <el-form-item label="分类名称">
          <el-input v-model="categoryForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="categoryForm.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单位">
          <el-input v-model="categoryForm.proUnit" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirm()">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import th from 'element-ui/src/locale/lang/th'
import el from 'element-ui/src/locale/lang/el'

export default {
  name: 'category',
  data () {
    return {
      data: [],
      defaultProps: {
        children: 'childrenCategories',
        label: 'name'
      },
      //设置自定义展开节点
      expandedKey: [],
      //弹窗名称
      title: '添加分类',
      //控制弹窗
      dialogFormVisible: false,
      //添加分类表单
      categoryForm: {
        name: "",
        parentId: 0,
        catLevel: 0,
        isShow: 1,
        sort: 0,
        proCount: 0,
        proUnit: "",
        icon: "",
        id: null
      }
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    batchDelete(){//批量删除
      var currentNode = this.$refs.categoryTree.getCheckedNodes();
      console.log("选中的节点=",currentNode)
      var ids = [];
      var categoryNames = [];//批量删除名称
      for (let i = 0; i < currentNode.length; i++) {
        ids.push(currentNode[i].id)
        categoryNames.push(currentNode[i].name)
      }
      console.log('ids=', ids)
      this.$confirm(`确定批量删除【${categoryNames}】菜单?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          //url: 'http://localhost:9090/commodity/category/delete',
          url: this.$http.adornUrl('/commodity/category/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log('remove_res=', data)
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
            })
            //刷新列表
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(()=>{})//confirm取消报错
    },
    clear(){//清空弹窗form
      this.categoryForm = {
        name: "",
        parentId: 0,
        catLevel: 0,
        isShow: 1,
        sort: 0,
        proCount: 0,
        proUnit: "",
        icon: "",
        id: null
      }
    },
    edit(data){//点击回显当前分类信息
      this.clear()
      //获取分类id
      let id = data.id
      this.$http({
        //url: 'http://localhost:9090/commodity/category/info/' + id,
        //url: 'http://localhost:9090/commodity/category/info/${id}' ,
        url: this.$http.adornUrl('/commodity/category/info/' + id),
        method: 'get',
      }).then(({data}) => {
        if (data && data.code === 0) {
          console.log('回显res=', data)
          this.categoryForm.icon = data.category.icon;
          this.categoryForm.name = data.category.name;
          this.categoryForm.id = data.category.id;
          this.categoryForm.proUnit = data.category.proUnit;
          this.categoryForm.parentId = data.category.parentId;
          // this.categoryForm.catLevel = data.category.catLevel;
          // this.categoryForm.isShow = data.category.isShow;
          // this.categoryForm.sort = data.category.sort;
          // this.categoryForm.proCount = data.category.proCount;

          //显示弹窗
          this.title = '修改分类'
          this.dialogFormVisible = true
        }
      })
    },
    confirm() {//添加-修改分类确认
      //可以用title === '添加分类'来判断
      if (this.categoryForm.id != null){//修改
        console.log("修改分类数据= " ,this.categoryForm)
        //对象解构
        var {icon,name,id,proUnit} = this.categoryForm
        this.$http({
          //url: 'http://localhost:9090/commodity/category/update',
          url: this.$http.adornUrl('/commodity/category/update'),
          method: 'post',
          data: this.$http.adornData( {icon,name,id,proUnit},false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log('res=', data)
            this.$message({
              message: '修改成功',
              type: 'success',
              duration: 1500,
            })
            //关闭弹窗
            this.dialogFormVisible = false
            //刷新列表
            this.expandedKey.push(this.categoryForm.parentId)
            this.getDataList()
          }
        })
      }else{//添加
        console.log("添加分类数据= " ,this.categoryForm)
        this.$http({
          url: this.$http.adornUrl('/commodity/category/save'),
          //url: 'http://localhost:9090/commodity/category/save',
          method: 'post',
          data: this.$http.adornData( this.categoryForm,false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log('res=', data)
            this.$message({
              message: '添加成功',
              type: 'success',
              duration: 1500,
            })
            //关闭弹窗
            this.dialogFormVisible = false
            //刷新列表
            this.expandedKey.push(this.categoryForm.parentId)
            this.getDataList()
          }
        })
      }
    },
    append (data) {//点击增加分类
      this.clear()
      console.log('data=', data)
      //数据处理
      this.categoryForm.parentId = data.id;
      //添加的分类为其父类的层级+1, 这里*1 是为了将字符串转成数值
      this.categoryForm.catLevel = data.catLevel * 1 + 1;
      this.title = '添加分类'
      this.dialogFormVisible = true
    },
    remove (node, data) {//点击移除分类
      console.log('node=', node)
      console.log('data=', data, 'id=' + data.id + ' name=' + data.name)

      //待删除id数组
      var ids = [data.id]
      console.log('ids=', ids)
      this.$confirm(`确定对【${data.name}】菜单进行删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          //url: 'http://localhost:9090/commodity/category/delete',
          url: this.$http.adornUrl('/commodity/category/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log('remove_res=', data)
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
            })
            //刷新列表
            this.getDataList()
            this.expandedKey.push(node.parent.data.id)
            //this.expandedKey = [node.parent.data.id]
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(()=>{})//confirm取消报错
    },
    handleNodeClick (data) {
      console.log(data)
    },
    // 获取分类列表-带有层级关系
    getDataList () {
      this.$http({
        url: this.$http.adornUrl('/commodity/category/list/tree'),
        //url: 'http://localhost:9090/commodity/category/list/tree',
        method: 'get',
      }).then(({data}) => {
        if (data && data.code === 0) {
          console.log('res=', data)
          this.data = data.data
        }
      })
    },
  }
}
</script>

<style scoped lang="scss">

</style>
